﻿using AutoMapper;
using AutoMapper.Internal.Mappers;
using ERP.Common.Results;
using ERP.ERPSystem.HumanResources.Dto.InterviewMenagementI;
using ERP.Team.HumanResources;
using ERP.Team.RBAC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace ERP.ERPSystem.HumanResources
{
    /// <summary>
    /// 面试管理服务实现类--bpy
    /// </summary>
    public class InterviewMenagementServices : ApplicationService, InterviewMenagementIServices
    {
        /// <summary>
        /// 面试管理仓储
        /// </summary>
        private readonly IRepository<InterviewMenagementModel, int> _InterviewMenagementIBps;
        /// <summary>
        /// 部门仓储
        /// </summary>
        private readonly IRepository<DepartmentModel, int> _departmentModelBps;


        public InterviewMenagementServices(IRepository<DepartmentModel, int> departmentModelBps, IRepository<InterviewMenagementModel, int> interviewMenagementIBps)
        {
            _departmentModelBps = departmentModelBps;
            _InterviewMenagementIBps = interviewMenagementIBps;
        }

        /// <summary>
        /// 面试面试信息(包括部门)
        /// </summary>
        /// <returns></returns>
        public async Task<List<InterviewMenagementIDto>> GetInterviewMenagementDept()
        {
            try
            {
                var InterviewMenagementModel = await _InterviewMenagementIBps.GetListAsync();
                var departmentModel = await _departmentModelBps.GetListAsync();

                var interviewMenagementIDto = from a in InterviewMenagementModel
                                              join b in departmentModel on a.DepartmentId equals b.Id
                                              select new InterviewMenagementIDto
                                              {
                                                  Id = a.Id,
                                                  PersonnelCode = a.PersonnelCode,
                                                  PersonnelName = a.PersonnelName,
                                                  Phone = a.Phone,
                                                  Sex = a.Sex,
                                                  DefiniteIntegral = a.DefiniteIntegral,
                                                  DepartmentId = b.Id,
                                                  DepartmentName = b.DepartmentName,
                                                  PostWage = a.PostWage,
                                                  RolesId = a.RolesId,
                                                  Interviewer = a.Interviewer,
                                                  InterviewsNumber = a.InterviewsNumber,
                                                  InterviewsTime = a.InterviewsTime,
                                              };


                return interviewMenagementIDto.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 面试面试信息(不包括部门)
        /// </summary>
        /// <returns></returns>
        public async Task<APIPaging<InterviewMenagementModel>> GetInterviewMenagementModel(int page = 1, int limit = 3)
        {
            try
            {
                APIPaging<InterviewMenagementModel> pageList = new APIPaging<InterviewMenagementModel>();
                var InterviewMenagementModel2 = await _InterviewMenagementIBps.GetQueryableAsync();// ((page - 1) * limit, limit, "CreatedAt DESC");
                var InterviewMenagementModel = InterviewMenagementModel2.ToList();
                pageList.PageCount= InterviewMenagementModel2.Count();
                pageList.ListDate = InterviewMenagementModel;
                pageList.PageIndex=Convert.ToInt32(Math.Ceiling(pageList.PageCount * 1.0 / limit));
                return pageList;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改面试管理信息
        /// </summary>
        /// <param name="interviewMenagementIDto"></param>
        /// <returns></returns>
        public async Task<int> AddInterviewMenagement(InterviewMenagementIDto interviewMenagementIDto)
        {
            try
            {
                var interviewMenagement = ObjectMapper.Map<InterviewMenagementIDto, InterviewMenagementModel>(interviewMenagementIDto);
                var i = await _InterviewMenagementIBps.InsertAsync(interviewMenagement);
                return i == null ? 0 : 1;

            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<DepartmentModel>> GetDepartmentModel()
        {
            try
            {
                var departmentModel = await _departmentModelBps.GetListAsync();
                return departmentModel;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 反馈面试管理信息
        /// </summary>
        /// <param name="interviewMenagementIDto"></param>
        /// <returns></returns>
        public async Task<int> UpdateInterviewMenagement(FeedbackInterviewMenagementDto feedbackInterviewMenagementDto)
        {
            try
            {
                var interviewMenagement = await _InterviewMenagementIBps.GetAsync(feedbackInterviewMenagementDto.Id);
                interviewMenagement.InterviewFeedback = feedbackInterviewMenagementDto.InterviewFeedback;
                interviewMenagement.InterviewResult = feedbackInterviewMenagementDto.InterviewResult;
                var i = await _InterviewMenagementIBps.UpdateAsync(interviewMenagement);
                return i == null ? 0 : 1;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 删除面试管理信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleleteInterviewMenagement(int id) {
            try
            {
                await _InterviewMenagementIBps.DeleteAsync(id);
                return 1;
            }
            catch (Exception)
            {

                return 0;
            }  
        }
    }
}
